package com.emm.yixun.estate.service.impl;
import java.util.List;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.emm.yixun.basic.enums.ModelEnum;
import com.emm.yixun.basic.service.ModelKeyService;
import com.emm.yixun.basic.service.ProjectBuildingService;
import com.emm.yixun.basic.service.ProjectCarareaService;
import com.emm.yixun.basic.service.ProjectCarportService;
import com.emm.yixun.basic.service.ProjectPremisesService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.basic.service.ProjectUnitService;
import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.model.ProjectBuilding;
import com.emm.yixun.common.model.ProjectCararea;
import com.emm.yixun.common.model.ProjectCarport;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.ProjectUnit;
import com.emm.yixun.common.model.dto.ProjectBuildingDto;
import com.emm.yixun.common.model.dto.ProjectCarareaDto;
import com.emm.yixun.common.model.dto.ProjectCarportDto;
import com.emm.yixun.common.model.dto.ProjectPremisesDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.dto.ProjectUnitDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectPremisesServiceFacade;

@Service("projectPremisesServiceFacade")
@WebService(name="com.emm.yixun.estate.service.ProjectPremisesServiceFacade")
public class ProjectPremisesServiceFacadeImpl implements ProjectPremisesServiceFacade{
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired
	private ProjectPremisesService service;

	
	@Autowired
	private ModelKeyService modelKeyService;
	@Autowired
	private ProjectBuildingService projectBuildingService;
	@Autowired
	private ProjectUnitService projectUnitService;
	@Autowired
	private ProjectRoomService projectRoomService;
	@Autowired
	private ProjectCarareaService projectCarareaService;
	@Autowired
	private ProjectCarportService projectCarportService;

	public Response<ProjectPremisesDto> save(ProjectPremisesDto dto) {
		Response<ProjectPremisesDto> response=new Response<ProjectPremisesDto>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("保存"));
				return response;
			}
			dto.setPremisesCode(modelKeyService.next(ModelEnum.PREMISES));
			ProjectPremisesDto mDto=service.create(dto);
			if(null==mDto){
				response.fail("80000", "保存数据错误");
				return response;
			}
			response.success(mDto);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("保存数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> updateByPrimaryKey(ProjectPremisesDto dto,Boolean selective) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==selective || null==dto || null==dto.getId()){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("更新"));
				return response;
			}
			
			if(selective){
				int count=service.update(dto);
				if(count>0){
					response.success(Integer.valueOf(count));
				}else{
					response.fail("90001", "更新数据失败");
				}
			}else{
				response.fail("90000", "暂未实现,修改参数selective=true即可");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("更新数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> deleteByPrimaryKey(Long id) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("删除"));
				return response;
			}
			
			ProjectPremises premises = service.selectById(id);
			//状态变为无效时,下属房源变为无效
			//修改楼栋
    		ProjectBuildingDto buildingDto = new ProjectBuildingDto();
    		buildingDto.setPremisesId(premises.getId());
    		List<ProjectBuilding> buildingList = projectBuildingService.selectListByDto(buildingDto);
    		String buildingIds = "";
    		for(ProjectBuilding building: buildingList){
    			if(StringUtils.isNotEmpty(buildingIds)){
    				buildingIds += ",";
    			}
    			buildingIds += building.getId();
    		}
    		if(StringUtils.isNotEmpty(buildingIds)){
    			buildingDto.setDelFlag(DelFlagEnum.DEL.getNo());
        		projectBuildingService.updates(buildingDto, buildingIds);
    		}
    		
    		
    		//修改单元
    		ProjectUnitDto unitDto = new ProjectUnitDto();
    		unitDto.setPremisesId(premises.getId());
    		List<ProjectUnit> unitList = projectUnitService.selectListByDto(unitDto);
    		String unitIds = "";
    		for(ProjectUnit unit: unitList){
    			if(StringUtils.isNotEmpty(unitIds)){
    				unitIds += ",";
    			}
    			unitIds += unit.getId();
    		}
    		if(StringUtils.isNotEmpty(unitIds)){
    			unitDto.setDelFlag(DelFlagEnum.DEL.getNo());
        		projectUnitService.updates(unitDto, unitIds);
    		}
    		
    		
    		//修改房间
    		ProjectRoomDto roomDto = new ProjectRoomDto();
    		roomDto.setPremisesId(premises.getId());
    		List<ProjectRoom> roomList = projectRoomService.selectListByDto(roomDto);
    		String roomIds = "";
    		for(ProjectRoom room: roomList){
    			if(StringUtils.isNotEmpty(roomIds)){
    				roomIds += ",";
    			}
    			roomIds += room.getId();
    		}
    		if(StringUtils.isNotEmpty(roomIds)){
    			roomDto.setDelFlag(DelFlagEnum.DEL.getNo());
        		projectRoomService.updates(roomDto, roomIds);
    		}
    		
    		
    		//修改车区
    		ProjectCarareaDto carareaDto = new ProjectCarareaDto();
    		carareaDto.setPremisesId(premises.getId());
    		List<ProjectCararea> carareaList = projectCarareaService.selectListByDto(carareaDto);
    		String carareaIds = "";
    		for(ProjectCararea cararea: carareaList){
    			if(StringUtils.isNotEmpty(carareaIds)){
    				carareaIds += ",";
    			}
    			carareaIds += cararea.getId();
    		}
    		if(StringUtils.isNotEmpty(carareaIds)){
    			carareaDto.setDelFlag(DelFlagEnum.DEL.getNo());
        		projectCarareaService.updates(carareaDto, carareaIds);
    		}
    		
    		
    		//修改车位
    		ProjectCarportDto carportDto = new ProjectCarportDto();
    		carportDto.setPremisesId(premises.getId());
    		List<ProjectCarport> carportList = projectCarportService.selectListByDto(carportDto);
    		String carportIds = "";
    		for(ProjectCarport carport: carportList){
    			if(StringUtils.isNotEmpty(carportIds)){
    				carportIds += ",";
    			}
    			carportIds += carport.getId();
    		}
    		if(StringUtils.isNotEmpty(carportIds)){
    			carportDto.setDelFlag(DelFlagEnum.DEL.getNo());
        		projectCarportService.updates(carportDto, carportIds);
    		}
    		
			
			ProjectPremisesDto mDto=new ProjectPremisesDto();
			mDto.setId(id);
			int count=service.destroy(mDto);
			if(count>0){
				response.success(Integer.valueOf(count));
			}else{
				response.fail("90001", "删除数据失败");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("删除数据错误：{}",e);
		}
		return response;
	}
	
    @Override
	public Response<ProjectPremises> findById(Long id) {
		Response<ProjectPremises> response=new Response<ProjectPremises>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("主键查询"));
				return response;
			}
			ProjectPremises entity=service.selectById(id);
			if(null==entity){
				response.fail("90001", "主键查询数据失败");
			}else{
				response.success(entity);;
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("主键查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public ResponseList<ProjectPremises> findByDto(ProjectPremisesDto dto) {
		ResponseList<ProjectPremises> response=new ResponseList<ProjectPremises>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			List<ProjectPremises> list=service.selectListByDto(dto);
			if(null==list){
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(list);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Page<ProjectPremises> findPageByDto(ProjectPremisesDto dto, Integer begin,Integer row) {
		Page<ProjectPremises> response=new Page<ProjectPremises>();
		try {
			if(null==dto || null==begin || null==row){
				response.setSuccess(false);
				response.setErrorMsg("参数不合法");
				return response;
			}
			Page<ProjectPremises> page = service.getPageModel(dto, begin, row);
			if(null==page.getRows()){
				response.setSuccess(false);
				response.setErrorMsg("分页数据不存在");
				return response;
			}
			response=page;
			response.setSuccess(true);
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询分页数据错误：{}",e);
		}
		return response;
	}

}
